using System;
using System.Windows;
using SvgToFontConverter.Models;

namespace SvgToFontConverter
{
    public partial class PerformanceSettingsWindow : Window
    {
        public PerformanceSettings Settings { get; private set; }

        public PerformanceSettingsWindow(PerformanceSettings currentSettings)
        {
            InitializeComponent();
            Settings = new PerformanceSettings
            {
                BatchSize = currentSettings.BatchSize,
                BatchDelayMs = currentSettings.BatchDelayMs,
                MemoryCleanupInterval = currentSettings.MemoryCleanupInterval,
                ProgressReportInterval = currentSettings.ProgressReportInterval,
                ProcessTimeoutSeconds = currentSettings.ProcessTimeoutSeconds,
                VerboseLogging = currentSettings.VerboseLogging
            };

            LoadCurrentSettings();
        }

        private void LoadCurrentSettings()
        {
            txtBatchSize.Text = Settings.BatchSize.ToString();
            txtBatchDelay.Text = Settings.BatchDelayMs.ToString();
            txtMemoryCleanupInterval.Text = Settings.MemoryCleanupInterval.ToString();
            txtProgressReportInterval.Text = Settings.ProgressReportInterval.ToString();
            txtProcessTimeout.Text = Settings.ProcessTimeoutSeconds.ToString();
            chkVerboseLogging.IsChecked = Settings.VerboseLogging;

            UpdateModeDescription();
        }

        private void HighPerformance_Click(object sender, RoutedEventArgs e)
        {
            var highPerfSettings = PerformanceSettings.GetHighPerformance();
            ApplySettings(highPerfSettings);
            UpdateModeDescription("高性能模式：大批处理，最小延迟，适合高配置电脑和大量文件处理。可能消耗更多内存和CPU资源。");
        }

        private void DefaultPerformance_Click(object sender, RoutedEventArgs e)
        {
            var defaultSettings = PerformanceSettings.GetDefault();
            ApplySettings(defaultSettings);
            UpdateModeDescription("默认模式：平衡的批处理大小和延迟，适合大多数情况。在性能和稳定性之间取得良好平衡。");
        }

        private void ConservativePerformance_Click(object sender, RoutedEventArgs e)
        {
            var conservativeSettings = PerformanceSettings.GetConservative();
            ApplySettings(conservativeSettings);
            UpdateModeDescription("保守模式：小批处理，较长延迟，适合低配置电脑或系统资源有限的情况。更稳定但处理速度较慢。");
        }

        private void ApplySettings(PerformanceSettings settings)
        {
            txtBatchSize.Text = settings.BatchSize.ToString();
            txtBatchDelay.Text = settings.BatchDelayMs.ToString();
            txtMemoryCleanupInterval.Text = settings.MemoryCleanupInterval.ToString();
            txtProgressReportInterval.Text = settings.ProgressReportInterval.ToString();
            txtProcessTimeout.Text = settings.ProcessTimeoutSeconds.ToString();
            chkVerboseLogging.IsChecked = settings.VerboseLogging;
        }

        private void UpdateModeDescription(string description = null)
        {
            if (description != null)
            {
                lblModeDescription.Text = description;
            }
            else
            {
                // 根据当前设置判断模式
                if (int.TryParse(txtBatchSize.Text, out int batchSize) && 
                    int.TryParse(txtBatchDelay.Text, out int batchDelay))
                {
                    if (batchSize >= 1000 && batchDelay <= 100)
                    {
                        lblModeDescription.Text = "当前设置接近高性能模式：大批处理，最小延迟。";
                    }
                    else if (batchSize <= 200 && batchDelay >= 500)
                    {
                        lblModeDescription.Text = "当前设置接近保守模式：小批处理，较长延迟。";
                    }
                    else
                    {
                        lblModeDescription.Text = "当前设置为自定义模式：根据您的需求调整的参数。";
                    }
                }
                else
                {
                    lblModeDescription.Text = "请输入有效的数值参数。";
                }
            }
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateAndSaveSettings())
            {
                DialogResult = true;
                Close();
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private bool ValidateAndSaveSettings()
        {
            try
            {
                // 验证并解析输入值
                if (!int.TryParse(txtBatchSize.Text, out int batchSize) || batchSize < 1 || batchSize > 5000)
                {
                    MessageBox.Show("批处理大小必须是1-5000之间的整数。", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtBatchSize.Focus();
                    return false;
                }

                if (!int.TryParse(txtBatchDelay.Text, out int batchDelay) || batchDelay < 0 || batchDelay > 10000)
                {
                    MessageBox.Show("批次间延迟必须是0-10000之间的整数。", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtBatchDelay.Focus();
                    return false;
                }

                if (!int.TryParse(txtMemoryCleanupInterval.Text, out int memoryCleanup) || memoryCleanup < 100 || memoryCleanup > 10000)
                {
                    MessageBox.Show("内存清理间隔必须是100-10000之间的整数。", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtMemoryCleanupInterval.Focus();
                    return false;
                }

                if (!int.TryParse(txtProgressReportInterval.Text, out int progressReport) || progressReport < 1 || progressReport > 1000)
                {
                    MessageBox.Show("进度报告间隔必须是1-1000之间的整数。", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtProgressReportInterval.Focus();
                    return false;
                }

                if (!int.TryParse(txtProcessTimeout.Text, out int processTimeout) || processTimeout < 30 || processTimeout > 3600)
                {
                    MessageBox.Show("进程超时时间必须是30-3600之间的整数。", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtProcessTimeout.Focus();
                    return false;
                }

                // 保存设置
                Settings.BatchSize = batchSize;
                Settings.BatchDelayMs = batchDelay;
                Settings.MemoryCleanupInterval = memoryCleanup;
                Settings.ProgressReportInterval = progressReport;
                Settings.ProcessTimeoutSeconds = processTimeout;
                Settings.VerboseLogging = chkVerboseLogging.IsChecked ?? false;

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存设置时发生错误：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }
    }
}